package com.dbf; 
import java.io.FileOutputStream; 
import java.io.OutputStream; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement;  
import com.linuxense.javadbf.DBFField; 
import com.linuxense.javadbf.DBFWriter; 
import com.mysql.jdbc.ResultSetMetaData; 
 
public class TestDBFWriter {
 
  private void test1(){ 
    try{ 
      Class.forName("com.mysql.jdbc.Driver"); 
      String url="jdbc:mysql://localhost:3306/hqcm";
      String username="root";
      String password="admin@pwd";
      Connection  conn  =  DriverManager.getConnection(url,username,password); 
      Statement stmt = conn.createStatement(); 
      ResultSet result = stmt.executeQuery("select * from datp0"); 
      ResultSetMetaData metaData = (ResultSetMetaData) result.getMetaData(); 
      OutputStream fos = null; 
      DBFField[] fields = new DBFField[metaData.getColumnCount()-3]; 
      Object[] vals =    new Object[metaData.getColumnCount()-3]; 
      DBFWriter dbfWriter = new DBFWriter(); 
      dbfWriter.setCharactersetName("gbk");
//           dbfWriter. 
      for(int i=4; i<=metaData.getColumnCount(); i++) { 
                fields[i-4] = new DBFField(); 
                fields[i-4].setName(metaData.getColumnName(i)); 
                if(metaData.getColumnTypeName(i).equalsIgnoreCase("INT")) { 
                    fields[i-4].setDataType(DBFField.FIELD_TYPE_N); 
                    vals[i-4] = "1"; 
                }else if(metaData.getColumnTypeName(i).equalsIgnoreCase("CHAR")) { 
                    fields[i-4].setDataType(DBFField.FIELD_TYPE_C); 
                    vals[i-4] = "1"; 
                }else if(metaData.getColumnTypeName(i).equalsIgnoreCase("VARCHAR")) { 
                    fields[i-4].setDataType(DBFField.FIELD_TYPE_C); 
                    vals[i-4] = "1"; 
                }else if(metaData.getColumnTypeName(i).equalsIgnoreCase("DATETIME")) { 
                    fields[i-4].setDataType(DBFField.FIELD_TYPE_D); 
                    vals[i-4] = "2011-12-28 15:30:30"; 
                }else if(metaData.getColumnTypeName(i).equalsIgnoreCase("DATE")) { 
                    fields[i-4].setDataType(DBFField.FIELD_TYPE_D); 
                    vals[i-4] = "2011-12-28"; 
                }else if(metaData.getColumnTypeName(i).equalsIgnoreCase("DECIMAL")) { 
                        fields[i-4].setDataType(DBFField.FIELD_TYPE_N); 
                        vals[i-4] = "1.11"; 
                }else { 
                    fields[i-4].setDataType(DBFField.FIELD_TYPE_F); 
                    vals[i-4] = "1"; 
                } 
                if(metaData.getColumnTypeName(i).equalsIgnoreCase("DECIMAL")){ 
                    fields[i-4].setFieldLength(metaData.getColumnDisplaySize(i)); 
                    fields[i-4].setDecimalCount(2); 
                } 
                else  if(!(metaData.getColumnTypeName(i).equalsIgnoreCase("DATE")  || 
                		metaData.getColumnTypeName(i).equalsIgnoreCase("DATETIME"))) { 
                    fields[i-4].setFieldLength(metaData.getColumnDisplaySize(i)); 
                } 
      } 
      dbfWriter.setFields(fields); 
//            dbfWriter.addRecord(vals); 
      String fn = "c:/hqcmtrandemo.dbf"; 
      fos = new FileOutputStream(fn); 
      dbfWriter.write(fos); 
      fos.close(); 
      System.out.println("dbf文件成功输出到："+fn); 
    }catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 
   
  public static void main(String[] args) { 
    TestDBFWriter inst = new TestDBFWriter(); 
    inst.test1(); 
  } 
   
}